package worker;

import java.util.Date;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.SendMessageRequest;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import controller.Utils;
import data.Data;

public class GetUrl extends Thread {
	DBObject obj;
	public GetUrl(DBObject obj) {

		this.obj = obj;
		this.start();
	}

	public void run(){
		try {
			String charset   = "UTF-8";	
			
			Document doc = null;
			
			String link = "";
			String type = obj.get("type").toString();
			String reglink = obj.get("reglink").toString();
			
			try {
				if(type.equals("html"))
				{
					doc = Jsoup.parse(Utils.GetHtml(obj.get("sitemap").toString(), charset));
					String[] arrreglink = reglink.split("\\|");
					if(arrreglink.length>=2)
						link = doc.select(arrreglink[0]).get(Integer.parseInt(arrreglink[1])).attr("href");
					else
						link = doc.select(reglink).get(0).attr("href");
						
				}
				else
				{
					doc = Jsoup.parse(Utils.GetHtml(obj.get("sitemap").toString(), charset).replaceAll("<link>", "<links>").replaceAll("</link>", "</links>"));
					link = doc.select("item").get(0).select("links").get(0).text();
				}
				
				link=Utils.fomatLink(link, obj.get("sitemap").toString());
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			if(!link.equals(""))
			{
				if(obj.get("link")==null || (obj.get("link")!=null && !obj.get("link").toString().equals(link)))
				{
					DBCollection colls = Data.data("savelinks");
					DBObject check = colls.findOne(new BasicDBObject("link",link).append("username", obj.get("username").toString()).append("blogurl", obj.get("blogurl").toString()));
//					System.out.println(check);
					if(check==null)
					{
						obj.put("link", link);
						obj.put("active", "1");
						DBCollection coll = Data.data("urlCraw");
						coll.save(obj);
						
						DBCollection collss = Data.data("logtest");
						BasicDBObject dbObjects = new BasicDBObject();
						dbObjects.put("link", "1");
						collss.insert(dbObjects);
//						System.out.println("1");
						BasicDBObject dbObject = new BasicDBObject();
						dbObject.put("link", link);
						dbObject.put("username", obj.get("username").toString());
						dbObject.put("blogurl", obj.get("blogurl").toString());
						colls.insert(dbObject);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
